package com.cbd.basic.leetcode.easy;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 有效的括号
 */
@Slf4j
public class Solution20 {
    public static void main(String[] args) {
        String s = "[]{}([])";
        boolean result = isValid(s);
        log.info("result = {}", result);
    }

    public static boolean isValid(String s) {
        if (s.length() % 2 == 1) {
            return false;
        }

        Stack<Character> stack = new Stack<>();
        Map<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('[',']');
        map.put('{', '}');

        for (int i = 0; i < s.length(); i++) {
            Character character = s.charAt(i);
            if (map.containsKey(character)) {
                stack.push(character);
            } else if (stack.isEmpty() || !character.equals(map.get(stack.pop()))) {
                return false;
            }
        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
